import settle from 'axios/lib/core/settle'
import buildURL from 'axios/lib/helpers/buildURL'
import buildFullPath from 'axios/lib/core/buildFullPath'

const formatConfig = (config, resolve, reject) => {
  return {
    url: buildURL(buildFullPath(config.baseURL, config.url), config.params, config.paramsSerializer),
    data: config.data || {},
    header: config.headers,
    method: config.method.toUpperCase(),
    timeout: config.timeout,
    complete: res => {
      settle(resolve, reject, {
        data: res.data,
        status: res.statusCode ?? 500,
        statusText: res.errMsg,
        headers: res.header,
        config: config,
        request: null
      })
    }
  }
}

export default config => {
  return new Promise((resolve, reject) => {
    uni.request(formatConfig(config, resolve, reject))
  })
}
